package com.java46.struts2.order.service;

import com.java46.struts2.base.entity.PageEntity;
import com.java46.struts2.base.entity.TOrderEntity;
import com.java46.struts2.base.exception.BusinessException;
import com.java46.struts2.base.util.JsonUtils;
import com.java46.struts2.base.util.SqlUtils;
import com.java46.struts2.order.dao.OrderDao;
import com.java46.struts2.orderdetails.dao.OrderDetailsDao;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderService
 * @Description 订单逻辑处理
 * @Author zuxia
 * @Date 2018/7/18 9:51
 * @Vcrsion 1.0
 **/
public class OrderService {

    /**
    *@Author xiao
    *@Description //分页查询订单可加关键字
    *@Date 9:53 2018/7/18
    *@Param [Pageno,Pagesize,Keyword]
    *@return PageEntity
    */
    public static PageEntity queryOrder(String Pageno, String Pagesize, String Keyword){
        int pagesize=0;
        int pageno=0;
        //如果为空
        if (Pagesize==null||Pagesize.trim().equals("")){
            pagesize=8;
        }else {
            try {
                //转换出错
                pagesize=Integer.parseInt(Pagesize);
            }catch (Exception e){
                pagesize=8;
            }
        }

        try{
            //转换出错
            pageno=Integer.parseInt(Pageno);
        }catch (Exception e){
            pageno=1;
        }

        List list = OrderDao.queryOrder(Keyword,pageno,pagesize);
        if(list.isEmpty()){
            throw new BusinessException("没有查询到任何订单数据");
        }
        PageEntity page=new PageEntity();
        //数据集合
        page.setList(list);
        int count=OrderDao.queryOrderCount(Keyword);
        //总条数
        page.setTotalNumber(count);
        //页码
        page.setPageNo(pageno);
        //每页条数
        page.setPageSize(pagesize);
        //总页数
        page.setPageCount((page.getTotalNumber()%page.getPageSize()==0)?page.getTotalNumber()/page.getPageSize():page.getTotalNumber()/page.getPageSize()+1);
        return page;
    }


    /**
    *@Author xiao
    *@Description //根据订单编号删除订单
    *@Date 11:57 2018/7/18
    *@Param [orderid]//订单编号
    *@return void
    */
    public static void deleteOrder(String orderid){
        int orderId;
        if(orderid==null||orderid.trim().equals("")){
            throw new BusinessException("订单编号为空");
        }
        try {
            orderId=Integer.parseInt(orderid);
        }catch (Exception e){
            throw  new BusinessException("订单编号格式不正确");
        }
        //删除订单表里的数据
        OrderDao.deleteOrderOrderid(orderId);


    }

    /**
     * @Author xiaobo
     * @Description //查询用户订单
     * @Date 23:16 2018/7/19 0019
     * @Param [uSign, struts]
     * @return java.util.List
     */
    public static List queryUserOrder(String uId,String Struts){
        int struts=0;
        int uid=0;
        if(uId==null||uId.trim().equals("")||Struts==null||Struts.trim().equals("")){
            throw new BusinessException("用户信息为空");
        }
        try {
            struts=Integer.parseInt(Struts);
            uid=Integer.parseInt(uId);
        }catch (Exception e){
            throw new BusinessException("用户信息格式不正确");
        }
        List list = OrderDao.queryUserOrder(uid, struts);
        return list;
    }

    /**
    *@Author xiao
    *@Description //生成订单
    *@Date 9:23 2018/7/21
    *@Param [uId, tableNo, pNum, remark, totalMoney, dishesStr]
    *@return java.lang.String
    */
    public static String addUserOrder(String uId,String tableNo,String pNum,String remark,String totalMoney,String dishesStr){

        if (uId==null||uId.trim().equals("")||tableNo==null||tableNo.trim().equals("")||pNum==null||pNum.trim().equals("")||totalMoney==null||totalMoney.trim().equals("")||dishesStr==null||dishesStr.trim().equals("")){
            throw new BusinessException("用户订单数据不完整");
        }
        int uid=0;
        int tableno=0;
        int pnum=0;
        BigDecimal totalmoney=null;
        try {
            uid=Integer.parseInt(uId);
            tableno=Integer.parseInt(tableNo);
            pnum=Integer.parseInt(pNum);
            totalmoney=new BigDecimal(totalMoney);
        }catch (Exception e){
            throw  new BusinessException("用户订单数据格式不正确");
        }
        TOrderEntity orderEntity=new TOrderEntity();
//        orderEntity.setOrderId(0);
        orderEntity.setOrderNo(SqlUtils.getOrderIdByUUId("fullboy"));
        orderEntity.setpNum(pnum);
        orderEntity.setRemark(remark);
        orderEntity.setTableNo(tableno);
        orderEntity.setTotalMoney(totalmoney);
        long dataTime = new Date().getTime();
        orderEntity.setCreateTime(new Timestamp(dataTime));
        orderEntity.setuId(uid);
        orderEntity.setStruts(0);
        List list = JsonUtils.getList(dishesStr);

        String orderno = OrderDao.addOrder(orderEntity, list);
        return orderno;
    }

    /**
    *@Author xiao
    *@Description //根据订单号查询订单信息
    *@Date 18:56 2018/7/20
    *@Param [orderNo]
    *@return java.util.List
    */
    public  static List queryOrderNO(String orderNo){
        if(orderNo==null||orderNo.trim().equals("")){
            throw new BusinessException("订单号不能为空");
        }
        List list = OrderDao.queryOrderNo(orderNo);
        return list;
    }

    /**
    *@Author xiao
    *@Description //修改订单状态
    *@Date 19:13 2018/7/20
    *@Param []
    *@return void
    */
    public static void updateOrderState(String orderId,String payType,String Struts){

        if (orderId==null||orderId.trim().equals("")||Struts==null||Struts.trim().equals("")){
            throw new BusinessException("订单编号或状态不能为空");
        }

        int paytype=9;
        int struts;
        int orderid;
        try{
            struts =Integer.parseInt(Struts);
            orderid =Integer.parseInt(orderId);
        }catch (Exception e){
            throw new BusinessException("状态信息格式不正确");
        }
        try{
            paytype =Integer.parseInt(payType);
        }catch (Exception e){
            e.printStackTrace();
        }

        OrderDao.updateOrderState(orderid,paytype,struts);

    }
}
